/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.properties.advanced;

import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.Package;

import com.cea.papyrus.core.ui.dialogs.ChooseAggregationKindDialog;
import com.cea.papyrus.core.ui.dialogs.ChooseAssociationDialog;
import com.cea.papyrus.core.ui.dialogs.ChooseDirectionDialog;
import com.cea.papyrus.core.ui.dialogs.ChooseRedefinedPropertiesDialog;
import com.cea.papyrus.core.ui.dialogs.ChooseSubsetPropertiesDialog;
import com.cea.papyrus.core.ui.dialogs.ChooseTypeDialog;
import com.cea.papyrus.core.ui.dialogs.ChooseVisibilityDialog;
import com.cea.papyrus.core.ui.dialogs.IChooseDialog;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.core.utils.LookForElement;


/**
 * a new Font dialog cell editor.
 */
public class ChooseCellEditor extends DialogCellEditor  {
	
	/**
	 * 
	 */
	protected IChooseDialog cDialog;
	
	/**
	 * 
	 */
	protected Class dialog;
	
	/**
	 * 
	 */
	protected org.eclipse.uml2.uml.Element currentelement;

	/** Debug channel */
	protected final int channel = 21;
	
    /**
     * Creates a new Font dialog cell editor parented under the given control.
     * The cell editor value is <code>null</code> initially, and has no
     * validator.
     * 
     * @param dialog 
     * @param currentElement 
     * @param parent the parent control
     */
    protected ChooseCellEditor(Composite parent,Class dialog,
    		org.eclipse.uml2.uml.Element currentElement) {
        super(parent);
        this.dialog=dialog;
        this.currentelement=currentElement;
    }

    /**
     * 
     * 
     * @param cellEditorWindow 
     * 
     * @return 
     * 
     * @see org.eclipse.jface.viewers.DialogCellEditor#openDialogBox(Control)
     */
    @Override
	protected Object openDialogBox(Control cellEditorWindow) {
    	
    	Package model=LookForElement.topPackage(currentelement);
    	
		//VisibilityKind
		if (dialog==ChooseVisibilityDialog.class){
    	cDialog = new ChooseVisibilityDialog(PlatformUI.getWorkbench()
            .getActiveWorkbenchWindow().getShell());
    	}
		//Direction of a parameter
    	else if(dialog==ChooseDirectionDialog.class){
        	cDialog = new ChooseDirectionDialog(PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getShell());
        }
		//Type 
    	else if(dialog==ChooseTypeDialog.class){
        	cDialog = new ChooseTypeDialog(PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getShell(),model);
        }
		//Choose Associration
    	else if(dialog==ChooseAssociationDialog.class){
        	cDialog = new ChooseAssociationDialog(PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getShell(),model);
        }
		
		//Choose type of aggration
    	else if(dialog==ChooseAggregationKindDialog.class){
        	cDialog = new ChooseAggregationKindDialog(PlatformUI.getWorkbench()
                .getActiveWorkbenchWindow().getShell());
        }
		//Choose set of Properties
    	else if(dialog==ChooseSubsetPropertiesDialog.class){
    		cDialog  = new ChooseSubsetPropertiesDialog(
    				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                (org.eclipse.uml2.uml.Property)currentelement);
         
    	}
		//Choose set of Properties
    	else if(dialog==ChooseRedefinedPropertiesDialog.class){
    		cDialog  = new ChooseRedefinedPropertiesDialog(
    				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                (org.eclipse.uml2.uml.Property)currentelement);
         
    	}
    	else{
    		Debug.error(this, "error: bad choose dialog at"+this.getClass()+":"+ dialog, channel);}

        cDialog.open();
        
        return getValue();
        //value;
    }
    
    /**
     * 
     * 
     * @return 
     */
    @Override
	protected Object doGetValue(){
    	if (cDialog==null) {
			return null;
		}
    	
    	return cDialog.getValue();
    }
    

}
